package ee.sedelitarkvara.util;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Logger {
	/**
	 * Logs important stuff to a file.
	 */
	private boolean logging;
	
	public Logger(boolean logging){
		this.logging = logging;
	}
	
	public void log(String data){
		if(logging){
			DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
			Date date = new Date();
			try{
				// Create file 
				FileWriter fstream = new FileWriter("LOG.txt",true);
				BufferedWriter out = new BufferedWriter(fstream);
				out.write(dateFormat.format(date)+" - "+data+"\r\n");
				//Close the output stream
				out.close();
			}catch (Exception e){//Catch exception if any
				System.err.println("Error: " + e.getMessage());
			}
		}
	}
	
}
